function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds();

  return [year, month, day].map(formatNumber).join('/')
  // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
}
//时间戳格式化用于生成请求时间参数
function formatTimeStamp(date){
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return '' + y + m + d + h + minute + second;
}
//秒转日期
function formatSecond(data){
  if (typeof data == 'undefined'){
    data=0;
  }
  var hours = parseInt((data % (60 * 60 * 24)) / (60 * 60));
  var minutes = parseInt((data % (60 * 60)) / 60);
  var seconds = (data % 60);
 
  hours = hours > 9 ? hours : ("0" + hours);
  minutes = minutes > 9 ? minutes : ("0" + minutes);
  seconds = seconds > 9 ? seconds : ("0" + seconds);
  return hours + ":" +minutes + ":"+seconds
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//用于生成特定位数的随机数工具
function getRandom_str(length) {
  var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
  ALPHABET += '0123456789';
  var str = '';
  for (var i = 0; i < length; ++i) {
    var rand = Math.floor(Math.random() * ALPHABET.length);
    str += ALPHABET.substring(rand, rand + 1);
  }
  return str;
}
//生成请求的公共参数和签名，并进行参数的转换 
function reqProtocol(obj){
  var version = '1.0.0';
  var charset = 'UTF-8';
  //时间戳
  var requestTime = formatTimeStamp(new Date());
  var certSerialNo = 'a-z';
  //签名...
  var paramObj = {
    version:version,
    charset:charset,
    requestTime:requestTime,
    certSerialNo:certSerialNo
  }
  //遍历传递的对象
  Object.keys(obj).forEach(function(key){
     paramObj[key] = obj[key];
  })
  return paramObj;
}

//计算地图两坐标点之间的距离
//params:selfPoint:自己坐标 distPoint:目标点坐标
/**---------计算地图两个坐标点之间距离----------- */
function OD(a, b, c) {
  while (a > c) a -= c - b;
  while (a < b) a += c - b;
  return a;
}

function SD(a, b, c) {
  b != null && (a = Math.max(a, b));
  c != null && (a = Math.min(a, c));
  return a;
}

function getDistance(startPoi, endPoi) {
  var a_lat = startPoi.lat;//纬度
  var a_lng = startPoi.lng;//经度
  var b_lat = endPoi.lat;
  var b_lng = endPoi.lng;

  var a = Math.PI * OD(a_lng, -180, 180) / 180;
  var b = Math.PI * OD(b_lng, -180, 180) / 180;
  var c = Math.PI * SD(a_lat, -74, 74) / 180;
  var d = Math.PI * SD(b_lat, -74, 74) / 180;
  return 6370996.81 * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b - a));//单位为米

}
//百度地图坐标转为火星坐标
function bd09Togcj02(bd_lon,bd_lat){
  var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  var x = bd_lon - 0.0065;
  var y = bd_lat - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  var gg_lng = z * Math.cos(theta);
  var gg_lat = z * Math.sin(theta);
  return [gg_lng, gg_lat]
}
function timeToDate(time ,date){
  // var date = new Date()
  var year = date.getFullYear()+''
  var month = date.getMonth()+1
  var day = date.getDate()+''
  let timeArr = time.split(':')
  if(timeArr.length <3){
    timeArr[2] = '00'
  }
  let newDate = year +'-'+ month+'-'+day+' '+timeArr[0]+':'+timeArr[1]+':'+timeArr[2]
  // console.log(newDate)
  return new Date(newDate).getTime()
}

module.exports = {
  formatTime: formatTime,
  getRandom_str: getRandom_str,
  reqProtocol: reqProtocol,
  getDistance: getDistance,
  formatSecond:formatSecond,
  formatTimeStamp: formatTimeStamp,
  bd09Togcj02:bd09Togcj02,
  timeToDate: timeToDate,
}